<?php

class Wishlist_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function get_table() {
		$table = "Listings";

		return $table;
	}

	//we also can do
	//var $table = "dfasfs";
	//and inside each function, we will change $table by $this->table
	// Add an item to the cart
	function validate_add_cart_item($options, $id) {
		// Validate posted data, and then add the item!
		$id = $this->input->post('listingId'); // Assign posted product_id to $id
		$cty = $this->input->post('quantity'); // Assign posted quantity to $cty

		$results = $this->getListingById($options, $id);

		//Check if a row has matched our listing
		if (count($results) > 0) {
			//We have a match
			foreach ($results as $row) {
				$data = array(
                    'id' => $id,
                    'qty' => $cty,
                    'price' => $row['AskingPriceLocal'],
                    'name' => $row['ProjectName_1']
				);

				//Add the data to the cart using the insert function that is available because we loaded the cart library
				$this->cart->insert($data);

				return TRUE;
			}
		} else {
			//Nothing found! Return FALSE
			return FALSE;
		}
	}

	// Updated the shopping cart
	function validate_update_cart() {
		// Get the total number of items in cart
		$total = $this->cart->total_items();

		// Retrieve the posted information
		$item = $this->input->post('rowid');
		$qty = $this->input->post('qty');

		// Cycle true all items and update them
		for ($i = 0; $i < $total; $i++) {
			// Create an array with the products rowid's and quantities.
			$data = array(
                'rowid' => $item[$i],
                'qty' => $qty[$i]
			);

			// Update the cart with the new information
			$this->cart->update($data);
		}
	}

	function getListing_With_Where_Limit() {
		$sql = "SELECT DISTINCT PAG_TABLEXXX.total, PAG_TABLEXXX.Listing_ID, PAG_TABLEXXX.PropertyFor, PAG_TABLEXXX.StatusFID,
							PAG_TABLEXXX.CountryFID,PAG_TABLEXXX.CityFID,PAG_TABLEXXX.LocaltionFID,PAG_TABLEXXX.SubAreaFID,
							PAG_TABLEXXX.Tenure,PAG_TABLEXXX. StreetNameFID, PAG_TABLEXXX.ProjectNameFID,PAG_TABLEXXX.PropertyTypeFID,
							PAG_TABLEXXX.FloorArea_SM,PAG_TABLEXXX.FloorArea_SF,PAG_TABLEXXX.LandArea_SM,PAG_TABLEXXX.LandArea_SF,
							PAG_TABLEXXX.AskingPriceLocal,PAG_TABLEXXX.AskingPriceUSD,PAG_TABLEXXX.PriceInRMBPerSQMLocal, PAG_TABLEXXX.PriceInRMBPerSQMUSD,
							PAG_TABLEXXX.PriceUnit,PAG_TABLEXXX.ListingType,PAG_TABLEXXX.NewLaunch,PAG_TABLEXXX.Longitude, PAG_TABLEXXX.Latitude,
							PAG_TABLEXXX.Bedroom,PAG_TABLEXXX.Bathroom,PAG_TABLEXXX.MemberFID,PAG_TABLEXXX.ListedOnUtc,
							PAG_TABLEXXX.LanguageFID_1,PAG_TABLEXXX.City_1,PAG_TABLEXXX.Location_1,PAG_TABLEXXX.Subarea_1,
							PAG_TABLEXXX.ProjectName_1,PAG_TABLEXXX.StreetName_1,
							PAG_TABLEXXX.PropertyType_1,CAST(PAG_TABLEXXX.[Description_1] AS NVARCHAR(MAX)) AS [Description_1]
					FROM
						(
							SELECT count(1) over( partition by 1 ) as total,  ROW_NUMBER() OVER ( ORDER BY  Listing_ID DESC) as rownum, l.*
							
							FROM dbo.view_listings_search l WHERE (1 = 1) AND StatusFID = 1 AND l.CountryFID = 8) AS PAG_TABLEXXX 
					WHERE rownum > 0 AND rownum <= 999999999";
		$query = $this->db->query($sql);

		return $query;
	}

	function getListingWithWhere($options, $limit, $id) {
		$tsql_callSP = '{call sp_listings_search_by_map(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}';

		$params = array(
		array($options['country'], SQLSRV_PARAM_IN),
		array($options['list_type'], SQLSRV_PARAM_IN),
		array($options['propertyfor'], SQLSRV_PARAM_IN),
		array($options['propertytype'], SQLSRV_PARAM_IN),
		array($options['cities'], SQLSRV_PARAM_IN),
		array($options['locations'], SQLSRV_PARAM_IN),
		array($options['subarea'], SQLSRV_PARAM_IN),
		array($options['price_min'], SQLSRV_PARAM_IN),
		array($options['price_max'], SQLSRV_PARAM_IN),
		array($options['size_min'], SQLSRV_PARAM_IN),
		array($options['size_max'], SQLSRV_PARAM_IN),
		array($options['bedrooms'], SQLSRV_PARAM_IN),
		array($options['bathrooms'], SQLSRV_PARAM_IN),
		array($options['keyword'], SQLSRV_PARAM_IN),
		array($options['features'], SQLSRV_PARAM_IN),
		array($options['facilities'], SQLSRV_PARAM_IN),
		array(0, SQLSRV_PARAM_IN),
		array(999999, SQLSRV_PARAM_IN),
		array($options['orderby'], SQLSRV_PARAM_IN),
		array(1, SQLSRV_PARAM_IN),
		array($id, SQLSRV_PARAM_IN)
		);

		$results = $this->db->exec_query($tsql_callSP, $params);

		$data = array();
		if ($results) {
			while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {
				$data[] = $row;
			}
		}

		return $data;
	}

	function getListingById($options, $id) {
		$tsql_callSP = '{call sp_listings_search_by_map(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}';

		$params = array(
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(0, SQLSRV_PARAM_IN),
		array(999999, SQLSRV_PARAM_IN),
		array(NULL, SQLSRV_PARAM_IN),
		array(1, SQLSRV_PARAM_IN),
		array($id, SQLSRV_PARAM_IN)
		);

		$results = $this->db->exec_query($tsql_callSP, $params);

		$data = array();
		if ($results) {
			while ($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {
				$data[] = $row;
			}
		}

		return $data;
	}

	function get($order_by) {
		$table = $this->get_table();

		$this->db->order_by($order_by);
		$query = $this->db->get($table);

		return $query;
	}

	function get_with_limit($limit, $offset, $order_by) {
		$table = $this->get_table();
		$this->db->limit($limit, $offset);
		$this->db->order_by($order_by);
		$query = $this->db->query($table);

		return $query;
	}

	function get_where($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$query = $this->db->get($table);

		return $query;
	}

	function get_where_custom($col, $value) {
		$table = $this->get_table();
		$this->db->where($col, $value);
		$query = $this->db->get($table);

		return $query;
	}

	function _insert($data) {
		$table = $this->get_table();
		$this->db->insert($table, $data);
	}

	function _update($id, $data) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->update($table, $data);
	}

	function _delete($id) {
		$table = $this->get_table();
		$this->db->where('id', $id);
		$this->db->delete($table);
	}

	function count_where($column, $value) {
		$table = $this->get_table();
		$this->db->where($column, $value);
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();
		return $num_rows;
	}

	function count_all() {
		$table = $this->get_table();
		$query = $this->db->get($table);
		$num_rows = $query->num_rows();

		return $num_rows;
	}

	function get_max() {
		$table = $this->get_table();
		$this->db->select_max('id');
		$query = $this->db->get($table);
		$row = $query->row();
		$id = $row->id;
		return $id;
	}

	function _custom_query($mysql_query) {
		$query = $this->db->query($mysql_query);
		return $query;
	}

}

?>
